// import router from './router'
// import store from './store'
// import { Message } from 'element-ui'
// import NProgress from 'nprogress' // progress bar
// import 'nprogress/nprogress.css' // progress bar style
// import { getToken } from '@/utils/auth' // get token from cookie
// import getPageTitle from '@/utils/get-page-title'

// NProgress.configure({ showSpinner: false }) // NProgress Configuration

// const whiteList = ['/login'] // no redirect whitelist

// router.beforeEach(async(to, from, next) => {
//   // start progress bar
//   NProgress.start()

//   // set page title
//   document.title = getPageTitle(to.meta.title)

//   // determine whether the user has logged in
//   const hasToken = getToken()

//   if (hasToken) {
//     if (to.path === '/login') {
//       // if is logged in, redirect to the home page
//       next({ path: '/' })
//       NProgress.done()
//     } else {
//       const hasGetUserInfo = store.getters.name
//       if (hasGetUserInfo) {
//         next()
//       } else {
//         try {
//           // get user info
//           await store.dispatch('user/getInfo')

//           next()
//         } catch (error) {
//           // remove token and go to login page to re-login
//           await store.dispatch('user/resetToken')
//           Message.error(error || 'Has Error')
//           next(`/login?redirect=${to.path}`)
//           NProgress.done()
//         }
//       }
//     }
//   } else {
//     /* has no token*/

//     if (whiteList.indexOf(to.path) !== -1) {
//       // in the free login whitelist, go directly
//       next()
//     } else {
//       // other pages that do not have permission to access are redirected to the login page.
//       next(`/login?redirect=${to.path}`)
//       NProgress.done()
//     }
//   }
// })

// router.afterEach(() => {
//   // finish progress bar
//   NProgress.done()
// })

import router, { asyncRoutes } from './router'
import store from './store'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
NProgress.configure({ showSpinner: false }) // NProgress Configuration
import getPageTitle from "@/utils/get-page-title";

// 刷新页面也会进入路由守卫
router.beforeEach(async (to, from, next) => {
    // console.log(to, from, next);
    NProgress.start()
    // 1.如果有token,且跳转登录页,则跳转首页
    // 2.如果没有token,且跳转非登录页,则跳转登录页
    const token = store.state.user.token
    if (token && to.path === '/login') {
        next('/')
        NProgress.done()
    } else if (!token && !['/login', '/404'].includes(to.path)) {
        next('/login')
        NProgress.done()
    } else {
        if (!store.state.user.userInfo.userId && token) {
            // 获取用户个人信息
            await store.dispatch('user/getUserInfo')
            const menus = store.state.user.userInfo.roles.menus
            console.log(menus);
            console.log(asyncRoutes);
            // const filterMenu = menus.map(name => asyncRoutes.find(item => item.children[0].name === name))
            const filterRoutes = asyncRoutes.filter(route => {
                const routeName = route.children[0].name
                return menus.includes(routeName)
            })
            filterRoutes.push({ path: '*', redirect: '/404', hidden: true })
            router.addRoutes(filterRoutes)
            store.commit('menu/setMenuList', filterRoutes)
            // 解决刷新出现的白屏bug
            next({
                ...to, // next({ ...to })的目的,是保证路由添加完了再进入页面 (可以理解为重进一次)
                replace: true // 重进一次, 不保留重复历史
            })
        } else {
            next()
        }
    }
})

router.afterEach(() => {
    // finish progress bar
    NProgress.done()
})

router.beforeEach((to, from, next) => {
    document.title = getPageTitle(to.meta.title)
    next()
})